package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/travelComment")
public class TravelCommentController {
    @Autowired
    private ITravelCommentService travelCommentService;
    //获取所有数据并共享给页面
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelCommentQuery qo){
        Page<TravelComment> page = travelCommentService.query(qo);
        model.addAttribute("page",page);
        return "/travelComment/list";
    }
    //保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(TravelComment travelComment){
        if(StringUtils.hasLength(travelComment.getId())){
            travelCommentService.save(travelComment);
        }else {
            travelComment.setId(null);
            travelCommentService.save(travelComment);
        }
        return JsonResult.success();
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        travelCommentService.delete(id);

        return  JsonResult.success();
    }
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){
        String content = travelCommentService.get(id).getContent();
        return JsonResult.success(content);
    }
}
